package com.archstoday.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.entity.NewsListEntity;
import com.archstoday.utils.ProcessUtils;
import com.archstoday.utils.ToolUtils;
import com.base.activity.BaseFragment;
import com.base.utils.ImageUtil;
import com.base.utils.StringUtil;
import com.bumptech.glide.Glide;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 要闻列表适配器
 */
public class MainNewAdapter extends BaseAdapter {

    private Context mContext;
    private List<NewsListEntity.DataBean> mData;
    private BaseFragment.FragmentClick mClick;

    public MainNewAdapter(Context context, BaseFragment.FragmentClick click) {
        mContext = context;
        mClick = click;
    }

    public void setData(List<NewsListEntity.DataBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void addData(List<NewsListEntity.DataBean> data) {
        if (mData == null) {
            mData = data;
        } else {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    public List<NewsListEntity.DataBean> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public NewsListEntity.DataBean getItem(int i) {
        if (mData == null || i >= mData.size()) {
            return null;
        } else {
            return mData.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_new, null);
            holder.mUserLayout = (RelativeLayout) view.findViewById(R.id.item_user_layout);
            holder.mHeadImg = (CircleImageView) view.findViewById(R.id.item_head_img);
            holder.mNameTv = (TextView) view.findViewById(R.id.item_name_tv);
            holder.mDesTv = (TextView) view.findViewById(R.id.item_des_tv);
            holder.mTopicTv = (TextView) view.findViewById(R.id.item_topic_tv);
            holder.mTopImg = (ImageView) view.findViewById(R.id.item_top_img);
            holder.mRightImg = (ImageView) view.findViewById(R.id.item_right_img);
            holder.mTitleTv = (TextView) view.findViewById(R.id.item_title_tv);
            holder.mTimeTv = (TextView) view.findViewById(R.id.item_time_tv);
            holder.mCommentTv = (TextView) view.findViewById(R.id.item_comment_tv);
            holder.mFromTv = (TextView) view.findViewById(R.id.item_from_tv);
            holder.mFocus = (TextView) view.findViewById(R.id.id_for_focus);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.mFocus.setVisibility(View.INVISIBLE);

        //设置关注的监听事件
        holder.mFocus.setTag(i);
        holder.mFocus.setOnClickListener(mClick);

        if (StringUtil.equest(mData.get(i).getNewsType(), "1")) {
            holder.mUserLayout.setVisibility(View.GONE);
            holder.mTopImg.setVisibility(View.GONE);
            holder.mRightImg.setVisibility(View.VISIBLE);
            holder.mDesTv.setVisibility(View.GONE);

            ImageUtil.setCropBitmap(mContext, holder.mRightImg,
                    mData.get(i).getImgs(), R.mipmap.default_icon);
        } else if (StringUtil.equest(mData.get(i).getNewsType(), "2")) {
            holder.mUserLayout.setVisibility(View.GONE);
            holder.mTopImg.setVisibility(View.GONE);
            holder.mRightImg.setVisibility(View.GONE);
            holder.mDesTv.setVisibility(View.GONE);
        } else if (StringUtil.equest(mData.get(i).getNewsType(), "3")) {
            holder.mUserLayout.setVisibility(View.GONE);
            holder.mTopImg.setVisibility(View.VISIBLE);
            holder.mRightImg.setVisibility(View.GONE);
            holder.mDesTv.setVisibility(View.GONE);

            ImageUtil.setCropBitmap(mContext, holder.mTopImg,
                    mData.get(i).getImgs(), R.mipmap.default_icon);
        } else if (StringUtil.equest(mData.get(i).getNewsType(), "4")) {
            holder.mUserLayout.setVisibility(View.VISIBLE);
            holder.mTopImg.setVisibility(View.VISIBLE);
            holder.mRightImg.setVisibility(View.GONE);
            holder.mDesTv.setVisibility(View.VISIBLE);
            holder.mFocus.setVisibility(View.VISIBLE);

            if (mData.get(i).getIsArticleUserLiked() == 1) {
                holder.mFocus.setText(ProcessUtils.procesText("已关注"));

                holder.mFocus.setTextColor(mContext.getResources().getColor(R.color.black));
                holder.mFocus.setBackground(mContext.getResources().getDrawable(R.drawable.topic_has_focus));
            } else {
                holder.mFocus.setText(ProcessUtils.procesText("关注"));

                holder.mFocus.setTextColor(mContext.getResources().getColor(R.color.white));
                holder.mFocus.setBackgroundColor(mContext.getResources().getColor(R.color.green_text));
            }

            Glide.with(mContext).load(mData.get(i).getUserImg()).placeholder(R.mipmap.user_header_icon).error(R.mipmap.user_header_icon).into(holder.mHeadImg);

            ImageUtil.setCropBitmap(mContext, holder.mTopImg,
                    mData.get(i).getImgs(), R.mipmap.default_icon);
            holder.mNameTv.setText(mData.get(i).getUserName());

            String label = mData.get(i).getDisplayLabel();
            if (TextUtils.isEmpty(label) || "null".equalsIgnoreCase(label)) {
                holder.mTopicTv.setVisibility(View.GONE);
            } else {
                holder.mTopicTv.setText("#" + ProcessUtils.procesText(label) + "#");
            }

        } else {
            holder.mUserLayout.setVisibility(View.GONE);
            holder.mTopImg.setVisibility(View.GONE);
            holder.mRightImg.setVisibility(View.GONE);
            holder.mDesTv.setVisibility(View.GONE);
        }

        holder.mTitleTv.setText(mData.get(i).getTitle());
        holder.mFromTv.setText(mData.get(i).getSourceName());
        holder.mCommentTv.setText(String.valueOf(mData.get(i).getComments()));
        holder.mTimeTv.setText(ToolUtils.getCreatTime(mData.get(i).getUpdateTime()));
        holder.mTimeTv.setVisibility(StringUtil.isEmpty(mData.get(i).getUpdateTime())
                ? View.GONE : View.VISIBLE);

        return view;
    }

    class Holder {
        RelativeLayout mUserLayout;
        CircleImageView mHeadImg;
        TextView mNameTv, mDesTv, mTopicTv;
        ImageView mTopImg, mRightImg;
        TextView mTitleTv, mCommentTv, mTimeTv, mFromTv, mFocus;
    }
}